#!/bin/bash
# Setup a new user to use ENCFS home dir 
# Version 0.0.5

which encfs>/dev/null
if [ "$?" != "0" ]; then
	echo "Missing encfs program, please install the encfs package."
	exit 1
fi

echo "Enter the username (shortname):"
read USERNAME

echo "Enter $USERNAME password:"
read -s USERPASSWORD

BACKUPTAR="$USERNAME.tmp.tar.gz"
USERGROUP=`groups $USERNAME | awk '{ print $1 }'`

# If we have dscacheutil then find home dir, otherwise use /Users
USERHOMEPATH="/Users/"
which dscacheutil>/dev/null
if [ "$?" == "0" ]; then
	USERHOME=`dscacheutil -q user -a name $USERNAME | awk '$1=="dir:" { print $2 }'`
	if ! [ -e "$USERHOME" ]; then
		echo "Error $USERNAME home directory did not exist."
		exit 1
	fi
	PATHLENGTH=`expr ${#USERHOME}-${#USERNAME}`
	USERHOMEPATH=`echo ${USERHOME:0:$PATHLENGTH}`	
fi

setupFailed()
{
	echo "**************************************************"
	echo "Failed to setup user $USERNAME"
	echo "**************************************************"
	exit $1
}

checkFreeSpace()
{
	local FREE_SPACE=`df -k $USERHOMEPATH | grep /dev/ | awk '{print $4}'`
	local USER_SPACE=$((`du -k -s $USERHOMEPATH$USERNAME 2>/dev/null| awk '{print $1}'` + 1024))
	local MAX_SPACE=$(($USER_SPACE * 2))
	
	# add some extra space 100M for tmp usage
	MAX_SPACE=$(($MAX_SPACE + 102400))
	if [ "$MAX_SPACE" -gt "$FREE_SPACE" ]; then
		echo "Error: Not enough free space in $USERHOMEPATH to convert user $USERNAME"
		setupFailed 5
	fi
}

if [ $USERNAME == "" ]; then
	echo "Error Missing username."
	exit 1
fi

if ! [ -e "$USERHOMEPATH$USERNAME" ]; then
	echo "Error: User name did not match name in $USERHOMEPATH looking for $USERHOMEPATH$USERNAME"
	setupFailed 2
fi

if [ -e "$USERHOMEPATH.$USERNAME" ]; then
	echo "Error: FileVault or Encfs data already exits for this users"
	setupFailed 5
fi

echo "You may be prompted for your current (admin) password."
cd $USERHOMEPATH

checkFreeSpace
echo "Backing up $USERNAME files"
sudo tar -czf $BACKUPTAR $USERNAME
if [ "$?" != 0 ]; then 
	echo "Error: tar failed to make an backup, exiting..."
	sudo rm $BACKUPTAR
	setupFailed 3
fi
sudo rm -rf    			$USERNAME
sudo mkdir     			.${USERNAME} $USERNAME
sudo chmod 700 			.${USERNAME} $USERNAME
sudo chown -R $USERNAME:$USERGROUP	.${USERNAME} $USERNAME

echo "Now create the new EncFS dir and mount it $USERHOMEPATH${USERNAME}"
echo "**************************************************"
echo "Configuring Encfs with the follwoing options"
echo "cipher algorithms = AES (option 1)"
echo "key size = 256"
echo "filesystem block size = 512 (default)"
echo "encoding algorithms = Block (option 1)"
echo "filename initialization vector chaining = yes (default)"
echo "per-file initialization vectors = yes (default)"
echo "filename to IV header chaining = no (default)"
echo "block authentication code headers = no (default)"
echo "**************************************************"
sudo encfs -S $USERHOMEPATH.${USERNAME} $USERHOMEPATH${USERNAME} -- -oallow_other  << EOF
x
1
256
512
1
yes
yes
no
no
$USERPASSWORD
$USERPASSWORD
EOF
if [ "$?" != 0 ]; then 
	USERPASSWORD="foo"
	echo "Error: encfs failed to create the EncFS mount."
	echo "Restoring $USERNAME files"
	sudo rm -rf .$USERNAME
	sudo sh -c "tar -xzf $BACKUPTAR"
	sudo rm $BACKUPTAR
	setupFailed 3
else
	USERPASSWORD="foo"
	echo "**************************************************"
	echo "Done with EncFS creation"
	echo "**************************************************"
fi
echo "Installing $USERNAME files into EncFS Volume"
SETUP_LEAVE_TAR="n"
sleep 2
sudo sh -c "tar -xzf $BACKUPTAR"
if [ "$?" != 0 ]; then 
	echo "**************************************************"
	echo "Warning: tar extract reported a problem, leaving file $BACKUPTAR"
	echo "Your home dir maybe missing files, please compare with the contents of the tar file."
	echo "You can remove the tar file when you are done."
	echo "**************************************************"
	SETUP_LEAVE_TAR="y"
fi

echo "Mark volume as Vault"
sudo touch $USERHOMEPATH${USERNAME}/.encfsvault
sudo chown $USERNAME:$USERGROUP $USERHOMEPATH${USERNAME}/.encfsvault

echo "Unmounting EncFS volume"
sudo umount $USERHOMEPATH${USERNAME}

echo "Finishing the install"
sudo chown $USERNAME:$USERGROUP $USERHOMEPATH.${USERNAME}/.encfs5
if [ "$SETUP_LEAVE_TAR" == "n" ]; then
	sudo rm $BACKUPTAR
fi

echo "Done"
